//
//  MainController.m
//  soccer
//
//  Created by 上官千城 on 15/9/11.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "MainController.h"

@interface MainController ()

@end

@implementation MainController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //创建表
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}


//相机按钮事件
-(void)CameraAction
{
    NSLog(@"相机");
    //创建弹出列表
    UIActionSheet *sheet;
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相册选取",@"取消", nil];
    }
    else {
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"从相册选取" otherButtonTitles:@"取消", nil];
    }
    //给列表一个tag值
    sheet.tag = 255;
    [sheet showInView:self.view];
}
//实现actionSheet delegate事件
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 255) {
        //数据类型 进行相应操作
        NSUInteger sourceType = 0;
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 2:
                    // 取消
                    return;
                case 0:
                    // 相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    // 相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
            }
        }
        else {
            if (buttonIndex == 1) {
                
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        //UIImagePickerController协议
        imagePickerController.delegate = self;
        //允许编辑
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        //模态到相机或相册页面
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }
}
//实现ImagePicker delegate 事件
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{//模态消失
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:YES completion:^{}];
    /* 此处info 有六个值
     * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
     * UIImagePickerControllerOriginalImage;  // a UIImage 原始图片
     * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后图片
     * UIImagePickerControllerCropRect;       // an NSValue (CGRect)
     * UIImagePickerControllerMediaURL;       // an NSURL
     * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework
     * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo
     */
    // 保存图片至本地，方法见下文
    [self saveImage:image withName:@"currentImage.png"];
    
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
    
    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
    self.isFullScreen = NO;
    [self.imageView setImage:savedImage];
    self.imageView.tag = 10000;
    
}
//保存图片至沙盒
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    // 获取沙盒目录
    
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    // 将图片写入文件
    
    [imageData writeToFile:fullPath atomically:NO];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}
//实现点击图片预览功能，滑动放大缩小，带动画
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    self.isFullScreen = !self.isFullScreen;
    UITouch *touch = [touches anyObject];
    
    CGPoint touchPoint = [touch locationInView:self.view];
    
    CGPoint imagePoint = self.imageView.frame.origin;
    //touchPoint.x ，touchPoint.y 就是触点的坐标
    
    // 触点在imageView内，点击imageView时 放大,再次点击时缩小
    if(imagePoint.x <= touchPoint.x && imagePoint.x +self.imageView.frame.size.width >=touchPoint.x && imagePoint.y <=  touchPoint.y && imagePoint.y+self.imageView.frame.size.height >= touchPoint.y)
    {
        // 设置图片放大动画
        [UIView beginAnimations:nil context:nil];
        // 动画时间
        [UIView setAnimationDuration:1];
        
        if (self.isFullScreen) {
            // 放大尺寸
            
            self.imageView.frame = CGRectMake(0, 0, 320, 480);
        }
        else {
            // 缩小尺寸
            self.imageView.frame = CGRectMake(50, 65, 90, 115);
        }
        
        // commit动画
        [UIView commitAnimations];
        
    }
    
}

//tableview协议方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    if (indexPath.section == 0 && indexPath.row == 0 ) {
        cell.textLabel.text = @"头像";
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 80, 10, 60, 60)];
//        image.backgroundColor = [UIColor orangeColor];
        [image setImage:[UIImage imageNamed:@"2013103109282582218_14@2x"]];
        [cell addSubview:image];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.section == 0 && indexPath.row == 1) {
        cell.textLabel.text = @"ID";
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH - 120, 10, 100, 30)];
        label.textAlignment = NSTextAlignmentRight;
        label.text = @"48564031";
//        label.backgroundColor = [UIColor cyanColor];
        [cell addSubview:label];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    if (indexPath.section == 0 && indexPath.row == 2) {
        cell.textLabel.text = @"昵称";
        self.nickName = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH - 120, 10, 100, 30)];
        self.nickName.textAlignment = NSTextAlignmentRight;
//        label.backgroundColor = [UIColor blueColor];
        self.nickName.text = @"妙吉祥";
        [cell addSubview:self.nickName];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    if (indexPath.section == 0 && indexPath.row == 3) {
        cell.textLabel.text = @"二维码";
        UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 50, 10 , 30, 30)];
        //        view.backgroundColor = [UIColor blackColor];
        view.image = [UIImage imageNamed:@"erweima"];
        [cell addSubview:view];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        cell.textLabel.text = @"性别";
        UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 40, 10 , 30, 30)];
//        view.backgroundColor = [UIColor blackColor];
        view.image = [UIImage imageNamed:@"nan.jpg"];
        [cell addSubview:view];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    if (indexPath.section == 1 && indexPath.row == 1) {
        cell.textLabel.text = @"地域";
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH - 120, 10, 100, 30)];
//        label.backgroundColor = [UIColor blueColor];
        label.textAlignment = NSTextAlignmentRight;
        label.text = @"上海,嘉定";
        [cell addSubview:label];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    if (indexPath.section == 1 && indexPath.row == 2) {
        cell.textLabel.text = @"签名";
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(70, 10, WIDTH - 90, cell.frame.size.height-10)];
//        label.backgroundColor = [UIColor redColor];
        label.text = @"未填写";
        label.textAlignment = NSTextAlignmentRight;
        [cell addSubview:label];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    return cell;
}
//cell数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 4;
    }
    
    return 3;
    
}
//区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
//点击触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        NSLog(@"头像");
//       调用相机方法
        [self CameraAction];
    }
    if (indexPath.section == 0 && indexPath.row == 1) {
        NSLog(@"ID");
        IDViewController *view = [[IDViewController alloc]init];
        view.type = 1;
        [self.navigationController pushViewController:view animated:YES];
    }
    if (indexPath.section == 0 && indexPath.row == 2) {
        NSLog(@"昵称");
        IDViewController *view = [[IDViewController alloc]init];
        view.type = 2;
        view.nickDelegate = self;
        [self.navigationController pushViewController:view animated:YES];
    }
    if (indexPath.section == 0 && indexPath.row == 3) {
        NSLog(@"二维码");
        IDViewController *view = [[IDViewController alloc]init];
        view.type = 3;
        [self.navigationController pushViewController:view animated:YES];
    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        NSLog(@"性别");
        
        
        
    }
    if (indexPath.section == 1 && indexPath.row == 1) {
        NSLog(@"地域");
        IDViewController *view = [[IDViewController alloc]init];
        view.type = 4;
        [self.navigationController pushViewController:view animated:YES];
    }
    if (indexPath.section == 1 && indexPath.row == 2) {
        NSLog(@"签名");
        IDViewController *view = [[IDViewController alloc]init];
        view.type = 5;
        [self.navigationController pushViewController:view animated:YES];
    }
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 80;
    }
    return 50;
}

//传递昵称协议方法
-(void)bringNickname:(NSString *)nickName
{
    self.nickName.text = nickName;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
